package cn.dansj.controller;

import cn.dansj.common.request.annotation.http.GetMethod;
import cn.dansj.common.request.annotation.http.PostMethod;
import cn.dansj.common.request.annotation.http.RequestMappingMethod;
import cn.dansj.common.request.annotation.http.RequestParam;
import cn.dansj.common.utils.bean.CacheToken;
import cn.dansj.common.utils.bean.Return;
import cn.dansj.entity.CalendarEvent;
import cn.dansj.service.CalendarEventService;
import cn.dansj.common.utils.transfer.GetTime;
import cn.dansj.common.utils.transfer.RandomUtils;
import cn.dansj.common.utils.transfer.Verification;

import java.sql.Timestamp;

@RequestMappingMethod("/calendar")
public class CalendarEventController {
    private final CalendarEventService calendarEventService;

    public CalendarEventController(CalendarEventService calendarEventService) {
        this.calendarEventService = calendarEventService;
    }

    @GetMethod("/getCalendar")
    public Return<?> getCalendar(CacheToken cacheToken) {
        return Return.T(calendarEventService.getCalendarEvent(cacheToken.getSequence()));
    }

    @PostMethod("/save")
    public Return<?> save(@RequestParam CalendarEvent calendarEvent, CacheToken cacheToken) {
        final Timestamp now = GetTime.dbTimestamp();
        if (Verification.checkNull(calendarEvent.getSequence())) {
            calendarEvent.setSequence(RandomUtils.sequence());
        }
        if (Verification.checkNull(calendarEvent.getCrtTime())) {
            calendarEvent.setCrtTime(now);
        }
        calendarEvent.setUpdateTime(now);
        calendarEvent.setUserId(cacheToken.getSequence());
        calendarEventService.save(calendarEvent);
        return Return.SUCCESS;
    }

    @PostMethod("/delete")
    public Return<?> delete(@RequestParam String sequence) {
        calendarEventService.delete(sequence);
        return Return.SUCCESS;
    }
}
